import { Inject, Controller, Get, Post, Param } from '@midwayjs/core';
import { Context } from '@midwayjs/koa';
import { BookService } from '../../service/admin/book.service';
import { PetService } from '../../service/user/pet.service';
import * as fs from 'fs';
import * as path from 'path';


@Controller('/api/admin')
export class APIController {
  @Inject()
  ctx: Context;

  @Inject()
  bookService: BookService;
  @Inject()
  petService: PetService;

  @Get('/getAllbook', { description: '获得所有预约' })
  async getAllbook() {
    const result = await this.bookService.getAllbook()
    console.log("result",result);
    if(result.length!=0){
        for(var i = 0 ;i< result.length; i++){
            var id = result[i].petid
            const petinfo = await this.petService.petinfo(id)
            const fileName = petinfo.petpic.imageurl;
            const petpic = fs.readFileSync(path.join(__dirname, '../../../public/upload', fileName));
            result[i]["src"] = petpic
            result[i]["petinfo"] = petinfo 
        }
    }
    return { success: true, message: '所有预约查询成功', result }
  }

  @Post("/takeOrder/:id",{ description: '接单' }) 
  async takeOrder(@Param() Param){
    console.log("id",Param)
    const result = await this.bookService.takeOrder(Param.id)
    return { success: true, message: '接单成功', result }
  } 

}